all repos — website @ 3c25c02d670e1e95608312c59aa90fde5b508d4c

My official website.

src/pages/blog/[...slug].astro (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
---
import Breadcrumbs from '@/components/Breadcrumbs.astro'
import Container from '@/components/Container.astro'
import PostNavigation from '@/components/PostNavigation.astro'
import TableOfContents from '@/components/TableOfContents.astro'
import { badgeVariants } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Separator } from '@/components/ui/separator'
import Layout from '@/layouts/Layout.astro'
import { formatDate, parseAuthors, readingTime } from '@/lib/utils'
import { Icon } from 'astro-icon/components'
import { Image } from 'astro:assets'
import { type CollectionEntry, getCollection } from 'astro:content'

export async function getStaticPaths() {
  const posts = (await getCollection('blog'))
    .filter((post) => !post.data.draft)
    .sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
  return posts.map((post) => ({
    params: { slug: post.slug },
    props: post,
  }))
}
type Props = CollectionEntry<'blog'>

const posts = (await getCollection('blog'))
  .filter((post) => !post.data.draft)
  .sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())

function getPostIndex(slug: string): number {
  return posts.findIndex((post) => post.slug === slug)
}

function getNextPost(slug: string): Props | null {
  const postIndex = getPostIndex(slug)
  return postIndex !== -1 && postIndex < posts.length - 1
    ? posts[postIndex + 1]
    : null
}

function getPrevPost(slug: string): Props | null {
  const postIndex = getPostIndex(slug)
  return postIndex > 0 ? posts[postIndex - 1] : null
}

const currentPostSlug = Astro.params.slug
const nextPost = getNextPost(currentPostSlug)
const prevPost = getPrevPost(currentPostSlug)

const post = Astro.props
const { Content, headings } = await post.render()

const authors = await parseAuthors(post.data.authors ?? [])
---

<Layout
  title={post.data.title}
  description={post.data.description}
  image={post.data.image?.src ?? '/static/1200x630.png'}
>
  <Container class="flex flex-col gap-y-6">
    <Breadcrumbs
      items={[
        { href: '/blog', label: 'Blog', icon: 'lucide:archive' },
        { label: post.data.title, icon: 'lucide:file-text' },
      ]}
    />

    {
      post.data.image && (
        <Image
          src={post.data.image}
          alt={post.data.title}
          width={1200}
          height={630}
          class="object-cover"
        />
      )
    }

    <section class="flex flex-col gap-6 text-center">
      <div>
        <h1 class="mb-4 text-4xl font-bold leading-tight sm:text-5xl">
          {post.data.title}
        </h1>

        <div
          class="flex flex-wrap items-center justify-center gap-2 text-sm text-muted-foreground"
        >
          <div class="flex items-center gap-2">
            <span>{formatDate(post.data.date)}</span>
            <Separator orientation="vertical" className="h-4" />
            <span>{readingTime(post.body)}</span>
          </div>
        </div>
        <div class="mt-4 flex flex-wrap justify-center gap-2">
          {
            post.data.tags && post.data.tags.length > 0 ? (
              post.data.tags.map((tag) => (
                <a
                  href={`/tags/${tag}`}
                  class={badgeVariants({ variant: 'secondary' })}
                >
                  <Icon name="lucide:hash" class="size-3 -translate-x-0.5" />
                  {tag}
                </a>
              ))
            ) : (
              <span class="text-sm text-muted-foreground">
                No tags available
              </span>
            )
          }
        </div>
      </div>
    </section>

    {headings.length > 0 && <TableOfContents headings={headings} />}

    <article class="prose prose-neutral max-w-none dark:prose-invert">
      <Content />
    </article>

    <PostNavigation prevPost={prevPost} nextPost={nextPost} />
  </Container>

  <Button
    variant="outline"
    size="icon"
    className="group fixed bottom-8 right-8 z-50 hidden"
    id="scroll-to-top"
    title="Scroll to top"
    aria-label="Scroll to top"
  >
    <Icon
      name="lucide:arrow-up"
      class="mx-auto size-4 transition-all group-hover:-translate-y-0.5"
    />
  </Button>

  <script>
    document.addEventListener('astro:page-load', () => {
      const scrollToTopButton = document.getElementById('scroll-to-top')
      const footer = document.querySelector('footer')

      if (scrollToTopButton && footer) {
        scrollToTopButton.addEventListener('click', () => {
          window.scrollTo({ top: 0, behavior: 'smooth' })
        })

        window.addEventListener('scroll', () => {
          const footerRect = footer.getBoundingClientRect()
          const isFooterVisible = footerRect.top <= window.innerHeight

          scrollToTopButton.classList.toggle(
            'hidden',
            window.scrollY <= 300 || isFooterVisible,
          )
        })
      }
    })
  </script>
</Layout>